package com.xiaoq.social.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.manager.entity.MerchantMgrEntity;
import com.xiaoq.manager.service.IManagerAccountService;
import com.xiaoq.social.entity.FansTagEntity;
import com.xiaoq.social.service.IFansTagService;
import com.xiaoq.social.vo.FansTagVo;
import com.xiaoq.users.redis.RedisTemplate4JedisCluster;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 对粉丝的操作只有后端会出现在关注的时候不需要对粉丝进行操作
 * 
 * @author wang zhihao
 *
 */
@RestController
@RequestMapping("/api/v1/fanstag")
public class FansTagEndPoint {

	@Autowired
	private IFansTagService fansTagSer;
	
	@Autowired
	private IManagerAccountService managerAccountSer;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;

	private class FansTagsListResponse extends GenericListResponse<FansTagVo> {
	}

	private final static Logger LOG = LoggerFactory.getLogger(FansTagEndPoint.class);

	@ApiOperation(value = "获取商店粉丝列表", notes = "获取商店粉丝列表")
	@ApiResponses({ @ApiResponse(code = 200, response = FansTagsListResponse.class, message = "获取商店粉丝列表") })
	@GetMapping(value = "/ofshop", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getUserList(
			@RequestParam(value = "tag", required = false) @ApiParam(value = "输入tag就是按标签查，不过不输及时查询全部列表") List<String> tags,
			@ModelAttribute(value = "pvo") PagingVo pvo,HttpServletRequest request) {
		// 粉丝的等级应该是个实时的参数应该是每次查询都是不同
		// 应该给我提供一个查询讲解员和商店使用次数的借口
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		if (StringUtils.isNotEmpty(shopId)) {
			List<FansTagEntity> fansTagList = new ArrayList<>();
			fansTagList = fansTagSer.fansTagList(shopId, tags, pvo);
			List<FansTagVo> fansTagVoList = new ArrayList<>();
			FansTagVo fansTagVo = null;
			Long total = 0L;
			if (fansTagList != null) {
				for (FansTagEntity fansTag : fansTagList) {
					fansTagVo = new FansTagVo();
					BeanUtils.copyProperties(fansTag, fansTagVo);

					Long levelNums = 0L;
					// TODO 需要调一个查询讲解员和商店使用次数的接口我来判断粉丝等级
					String level = "匆匆而过";
					fansTagVo.setLevel(level);
					String mobile = fansTag.getUserMobile();
					mobile = mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); 
					fansTagVo.setUserMobile(mobile);
					fansTagVoList.add(fansTagVo);
				}
				total = (Long) pvo.getTc();
			}
			return FansTagsListResponse.listAndCount(fansTagVoList, total);
		} else {
			LOG.error("必须传关注目标的Id！");
			return GenericResponse.ng("必须传关注目标的Id！");
		}
	}
	
	@ApiOperation(value = "给商店粉丝打标签", notes = "给商店粉丝打标签")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "给商店粉丝打标签") })
	@PostMapping(value = "/{userId}/usertag", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addFansTags(
			@PathVariable(value="userId",required=false)@ApiParam(value="粉丝的userId",required=false)String userId,
			@RequestParam(value="tags",required=true)List<String> tags,HttpServletRequest request){
		String userId2 = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId2);
		if(StringUtils.isNotEmpty(userId)&&StringUtils.isNotEmpty(shopId)){
			boolean result = fansTagSer.addTag(shopId, userId, tags);
			if(result){
				return GenericResponse.ok();
			}
		}else{
			LOG.error("没有登录或者是没有传递目标Id!");
		}
		return GenericResponse.ng("标签没有添加成功呢！");
	}
	
	@ApiOperation(value = "获取商店粉丝总数", notes = "获取商店粉丝总数")
	@ApiResponses({ @ApiResponse(code = 200, response = FansTagsListResponse.class, message = "获取商店粉丝总数") })
	@GetMapping(value = "/fans", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getFansNum(HttpServletRequest request) {
		
		Long fansNum = null;
		String userId = (String) request.getAttribute("userId");
		MerchantMgrEntity merchantMgr = managerAccountSer.findById(userId);
		if(merchantMgr!=null){
			String shopId = merchantMgr.getShopId();
			fansNum = fansTagSer.findFansNum(shopId);
		}
		return GenericDataResponse.okWithData(fansNum);
		

	}

}
